package com.jlrdb.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.jlrdb.model.AnalogRadioBroadcasts;
import com.jlrdb.model.CellularBroadcast;
import com.jlrdb.model.CellularBroadcasts;
import com.jlrdb.model.DigitalRadioBroadcasts;
import com.jlrdb.model.TvBroadcast;
import com.jlrdb.model.TvBroadcasts;
import com.jlrdb.service.CellularBroadcastService;
import com.jlrdb.service.RadioBroadcastService;
import com.jlrdb.service.TvBroadcastService;

public class AllBroadcastServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		TvBroadcastService tvBroadcastService = new TvBroadcastService();
		CellularBroadcastService cellularBroadcastService = new CellularBroadcastService();
		RadioBroadcastService radioBroadcastService = new RadioBroadcastService();
		if (action.equals("viewAllBroadcasts"))
		{
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			String Role;
			String domain = "temp";
			String identity = user.getEmail();
			if (identity.contains("jaguarlandrover.com"))
				domain = "jaguarlandrover.com";
			if (domain.equalsIgnoreCase("jaguarlandrover.com"))
			{
				if (identity.equalsIgnoreCase("gagarwa2@jaguarlandrover.com") || identity.equalsIgnoreCase("rholenar@jaguarlandrover.com") || identity.equalsIgnoreCase("poreill3@jaguarlandrover.com")
						|| identity.equalsIgnoreCase("sarcot@jaguarlandrover.com"))
					Role = "Admin";
				else
					Role = "Viewer";
				HttpSession session = request.getSession();
				session.setAttribute("Role", Role);
				String country = request.getParameter("country");
				String criteria = "country";
				CellularBroadcasts cellularBroadcasts = new CellularBroadcasts();
				DigitalRadioBroadcasts digitalRadioBroadcasts = new DigitalRadioBroadcasts();
				AnalogRadioBroadcasts analogRadioBroadcasts = new AnalogRadioBroadcasts();
				TvBroadcasts tvBroadcasts = new TvBroadcasts();
				
				TvBroadcast tvQuery = new TvBroadcast();
				tvQuery.setCountry(country);
				CellularBroadcast cellularQuery = new CellularBroadcast();
				cellularQuery.setCountry(country);
				tvBroadcasts = tvBroadcastService.viewTvBroadcastsNew(tvQuery);
				digitalRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsDigital(criteria, country);
				analogRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsAnalog(criteria, country);
				cellularBroadcasts = cellularBroadcastService.viewCellularBroadcasts(cellularQuery);
				// request.setAttribute("geographicRegion", geographicRegion);
				request.setAttribute("country", country);
				request.setAttribute("tvBroadcasts", tvBroadcasts);
				request.setAttribute("cellularBroadcasts", cellularBroadcasts);
				request.setAttribute("digitalRadioBroadcasts", digitalRadioBroadcasts);
				request.setAttribute("analogRadioBroadcasts", analogRadioBroadcasts);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewAllFullDetails.jsp");
				rd.forward(request, response);
			} else
			{
				String message = "Please LOGIN with a valid JLR domain account.";
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/message.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("logout"))
		{
			HttpSession session = request.getSession();
			session.invalidate();
			System.out.println("logout");
			User user = null;
			response.sendRedirect("jsp/index.jsp");
		}
	}
}
